let mulit = require('m.js');
const randomString = a => {//32位字符串
  a = a || 32;
  const $chars = 'qwertyuiopasdfghjklzxcvbnm0123456789';
  const b = $chars.length;
  let c = '';
  for (let i = 0; i < a; i++) {
    c += $chars.charAt(Math.floor(Math.random() * b));
  }
  return c;
}
const objKeySort = (obj) => {//排序的函数
  let newArr = Object.keys(obj).sort();//先用Object内置类的keys方法获取要排序对象的属性名，再利用Array原型上的sort方法对获取的属性名进行排序
  let newObj = {};
  for (let i = 0; i < newArr.length;i++)
  {
    newObj[newArr[i]] = obj[newArr[i]];
  }
  return newObj;
}
const getString = (a) => {//a是对象
  let b = '';
  for(let key of Object.keys(a))
  {
    b+=a[key];
  }
  return b;
}

const getToken = (fn) => {//获取token
  return new Promise(function(resolve){
    const ttp = new Date().getTime();
    const sk =randomString(32);
    let objectB = {
      timestamps: ttp,
      signMark: sk,
      signNark: 'tc'
    }
    objectB = objKeySort(objectB);
    const str = mulit.hexMD5(encodeURIComponent(getString(objectB)));
    wx.request({
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      url: 'https://www.globalhexi.com/hexiMaoYi/webShop/hejiahe/web/getimtokenandcookieforjiami',
      data: { 'timestamps': ttp, 'signMark': sk, 'sign': str },
      method: 'POST',
      success: function (res) {
        console.log(res)
        wx.setStorageSync('cookie', res.data.N);
        wx.setStorageSync('token', res.data.M);
        let nowTime = Math.floor(Date.now() / 1000) + 86400;
        wx.setStorageSync('ntime', nowTime);
        resolve(res)
      }
    })
  })
}
const getOpenId = (fn) => {//获取openid
  wx.login({
    success: function (res) {
      const cookie = wx.getStorageSync('cookie') || '';
      const token = wx.getStorageSync('token') || '';
      const ttp = new Date().getTime();
      const sk = randomString(32);
      let objectB = {
        timestamps: ttp,
        signMark: sk,
        signNark: 'tc',
        cookie: cookie,
        token: token,
        project: 'gift',
        method: 'gift/messagePush/getSessionKey',
        param: '{"code":"' + res.code + '"}'
      }
      objectB = objKeySort(objectB);
      const str = mulit.hexMD5(encodeURIComponent(getString(objectB)));
      wx.request({
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        method: 'POST',
        url: 'https://www.globalhexi.com/hexiMaoYi/webShop/hejiahe/transferCtrl/transfer',
        data: { 'timestamps': ttp, 'signMark': sk, 'sign': str, 'cookie': cookie, 'token': token, 'project': 'gift', 'method': 'gift/messagePush/getSessionKey','param': '{"code":"' + res.code + '"}'},
        success: fn
      })
    }
  })
}
const getData = (fn, methods, param)=>{//不使用转发接口获取数据   
  const cookie = wx.getStorageSync('cookie') || '';
  const token = wx.getStorageSync('token') || '';
  const ttp = Date.now();
  const sk = randomString(32);
  let objectB = {
    timestamps: ttp,
    signMark: sk,
    signNark: 'tc',
    cookie:cookie,
    token: token
  }
  objectB = Object.assign(objectB, param);
  objectB = objKeySort(objectB);
  const str = mulit.hexMD5(encodeURIComponent(getString(objectB)));
  let objectC = { 'timestamps': ttp, 'signMark': sk, 'sign': str, 'cookie':cookie, 'token':token };
  objectC = Object.assign(objectC, param)
  wx.request({
    header: { 'content-type': 'application/x-www-form-urlencoded' },
    url: 'https://interface.globalhexi.cn/' + methods,
    data: objectC,
    method: 'POST',
    success: fn

  })
}
const pay = (fn,project, method, param) => {
  const cookie = wx.getStorageSync('cookie') || '';
  const token = wx.getStorageSync('token') || '';
  const ttp = new Date().getTime();
  const sk = randomString(32);
  let objectB = {
    timestamps: ttp,
    signMark: sk,
    signNark: 'tc',
    cookie:cookie,
    token:token,
    project: project,
    method: method,
    param: param
  }
  objectB = objKeySort(objectB);
  const str = mulit.hexMD5(encodeURIComponent(getString(objectB)));
  wx.request({
    header: { 'content-type': 'application/x-www-form-urlencoded' },
    url: 'https://www.globalhexi.com/hexiMaoYi/webShop/hejiahe/transferCtrl/transfer',
    data: { 'timestamps': ttp, 'signMark': sk, 'sign': str, 'cookie':cookie, 'token': token, project: project, method: method, param: param },
    method: 'POST',
    success: fn

   })
}
module.exports={
  getToken:getToken,
  getOpenId: getOpenId,
  getData:getData,
  pay:pay
}
